package com.net.shop.car.manager.ui.user;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.shop.car.manager.ActivityCollector;
import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.Constant;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.LookForPassword;
import com.net.shop.car.manager.api.volley.request.Register;
import com.net.shop.car.manager.api.volley.response.LookForPasswordResponse;
import com.net.shop.car.manager.api.volley.response.RegisterResponse;
import com.net.shop.car.manager.ui.MainActivity;

public class RegisterSureActivity extends BaseActivity {
	private EditText mEtPassword;
	private EditText mEtPasswordSure;
	private Button mBtnRegisterSure;
	private boolean couldRegister;
	private LinearLayout mLlTishi;
	private TextView mLlErrorText;
	private String strPsd;
	private String strPsdSure;
	private TextView titleTextView;
	private ImageView title_back_btn;
	private String res;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub


		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registersure);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub

		titleTextView = (TextView) findViewById(R.id.title_txt);
		title_back_btn = (ImageView) findViewById(R.id.title_back_btn);
		res = getIntent().getExtras().getString("res");
		if (res.equals("regist")) {
			titleTextView.setText("注册");
		} else {
			titleTextView.setText("找回密码修改");
		}
		title_back_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				finish();
			}
		});
		mEtPassword = (EditText) findViewById(R.id.password_et);
		mEtPasswordSure = (EditText) findViewById(R.id.password_sure_et);
		mBtnRegisterSure = (Button) findViewById(R.id.register_sure_btn);
		mLlTishi = (LinearLayout) findViewById(R.id.tishi_ll);
		mLlErrorText = (TextView) findViewById(R.id.error_text);
		mBtnRegisterSure.setOnClickListener(onClickListener);
		strPsd = mEtPassword.getText().toString();
		strPsdSure = mEtPasswordSure.getText().toString();
		mEtPasswordSure.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub


			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub


			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

				strPsdSure = mEtPasswordSure.getText().toString();
				if (strPsdSure.equals(strPsd)) {
					couldRegister = true;
					mLlTishi.setVisibility(View.INVISIBLE);
				} else {
					couldRegister = false;
					mLlTishi.setVisibility(View.VISIBLE);
					mLlErrorText.setText("密码不一致");
				}
			}
		});
		mEtPassword.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub


			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub


			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

				strPsd = mEtPassword.getText().toString();
				if (strPsdSure.equals(strPsd)) {
					couldRegister = true;
					mLlTishi.setVisibility(View.INVISIBLE);
				} else {
					couldRegister = false;
					mLlTishi.setVisibility(View.VISIBLE);
					mLlErrorText.setText("密码不一致");
				}
			}
		});
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub

			if (!couldRegister) {
				mEtPasswordSure.requestFocus();
				mLlTishi.setVisibility(View.VISIBLE);
				mLlErrorText.setText("密码不一致");
				return;
			}
			if (res.equals("regist")) {
				doRegist();
			} else {
				doRegetPsd();
			}

		}

	};

	protected void doRegetPsd() {
		// TODO Auto-generated method stub

		LookForPassword regist = new LookForPassword(getIntent().getExtras().getString("mobile"), mEtPassword.getText().toString(), getIntent().getExtras()
				.getString("msgyzm"));
		VolleyCenter.getVolley().addPostRequest(regist, new VolleyListenerImpl<LookForPasswordResponse>(new LookForPasswordResponse()) {

			@Override
			public void deleveryResponse(LookForPasswordResponse response) {
				if (response.isSuccess()) {
					actionStart(MainActivity.class);
					saveToSharedPreferences(Constant.sp.user_id, response.getMemberId());
					ActivityCollector.finishActivity("RegisterActivity");
					finish();
				} else {
					showToast(response.getErrorMsg());
				}
			}
		});

	}

	protected void doRegist() {
		// TODO Auto-generated method stub

		Register regist = new Register(getIntent().getExtras().getString("mobile"), mEtPassword.getText().toString(), getIntent().getExtras().getString(
				"msgyzm"));
		VolleyCenter.getVolley().addPostRequest(regist, new VolleyListenerImpl<RegisterResponse>(new RegisterResponse()) {

			@Override
			public void deleveryResponse(RegisterResponse response) {
				if (response.isSuccess()) {
					actionStart(MainActivity.class);
					saveToSharedPreferences(Constant.sp.user_id, response.getMemberId());
					ActivityCollector.finishActivity("RegisterActivity");
					finish();
				} else {
					showToast(response.getErrorMsg());
				}
			}
		});
	}
}
